package logic

import (
	"context"

	"friend/internal/svc"
	"friend/pb/friend"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetApplyListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetApplyListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApplyListLogic {
	return &GetApplyListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetApplyList 好友申请列表
func (l *GetApplyListLogic) GetApplyList(in *friend.ApplyListRequest) (*friend.ApplyListResponse, error) {
	total, err := l.svcCtx.FriendApplyExtendModel.Count(l.ctx, in.FromUserId, in.ToUserId)
	if err != nil {
		return nil, err
	}
	list, err := l.svcCtx.FriendApplyExtendModel.FindList(l.ctx, in.FromUserId, in.ToUserId, in.Offset, in.Limit)
	if err != nil {
		return nil, err
	}

	var data []*friend.ApplyInfo
	for _, v := range list {
		data = append(data, &friend.ApplyInfo{
			Id:         v.Id,
			FromUserId: v.FromUserId,
			ToUserId:   v.ToUserId,
			Message:    v.Message,
			Status:     v.Status,
			CreateAt:   v.CreatedAt.Unix(),
			UpdateAt:   v.UpdatedAt.Unix(),
		})
	}

	return &friend.ApplyListResponse{Total: total, Data: data}, nil
}
